Exercise 1: Ice Cream Cheat Sheet


In [1]:
price = 0.9
# Print the header    
print('Balls:    Price:        Balls:    Price:')

# Print prices in two columns
for balls in range(1,11):
    left = balls
    right = balls + 10
    skeletton = '{:^6}    {:^6.2f}        {:^6}    {:^6.2f}'
    print(skeletton.format(left, left*price, right, right*price))


Balls:    Price:        Balls:    Price:
  1        0.90           11       9.90 
  2        1.80           12      10.80 
  3        2.70           13      11.70 
  4        3.60           14      12.60 
  5        4.50           15      13.50 
  6        5.40           16      14.40 
  7        6.30           17      15.30 
  8        7.20           18      16.20 
  9        8.10           19      17.10 
  10       9.00           20      18.00 

Exercise 2: Body-Mass-Index


In [2]:
def get_category(bmi):
    '''
    A function which returns the category for a given bmi    
    '''
    if bmi < 15.0:
        return 'Very severely underweight'
    elif bmi < 16.0:
        return 'Severely underweight'
    elif bmi < 18.5:
        return 'Underweight'
    elif bmi < 25.0:
        return 'Normal'
    elif bmi < 30.0:
        return 'Overweight'
    elif bmi < 35.0:
        return 'Moderately obese'
    elif bmi < 40.0:
        return 'Severely obese'
    else:
        return 'Very severely obese'

In [3]:
def get_bmi(weight, height):
    '''
    This function takes the weight in [kg] and the height in [m] 
    It return the BMI. 
    '''
    return weight / height**2

In [4]:
def bmi_calculator(weight=None, height=None):
    '''
    A calculator which takes weight and heigt.
    It prints the bmi and corresponding category.
    If no or only one value is given it asks for both.
    '''
    if weight == None or height == None:
        print('This is a BMI calculator.')
    
        weight = float(input('Enter your weight (kg): '))
        height = float(input('Enter your height (m): '))
    else:
        status = 'Your weight is: {}\nYour height is: {}'
        print(status.format(weight, height))
        
    bmi = get_bmi(weight, height)
    category = get_category(bmi)
        
    skeletton = 'Your BMI is {:.1f}\nYour BMI category is {}'
    print(skeletton.format(bmi, category))


bmi_calculator(75, 1.74)
print('\n')
bmi_calculator(49, 1.85)
print('\n')
bmi_calculator(128, 1.71)


Your weight is: 75
Your height is: 1.74
Your BMI is 24.8
Your BMI category is Normal


Your weight is: 49
Your height is: 1.85
Your BMI is 14.3
Your BMI category is Very severely underweight


Your weight is: 128
Your height is: 1.71
Your BMI is 43.8
Your BMI category is Very severely obese

In [7]:
bmi_calculator(80,1.73)


Your weight is: 80
Your height is: 1.73
Your BMI is 26.7
Your BMI category is Overweight

Exercise 3: Sieve of Erathostenes


In [8]:
def primes(limit):
    '''
    A function implementing the sieve of erathostenes.
    It take an upper limit and returns all prime numbers
    under this limit.
    '''
    status = [True] * limit
    status[0] = status[1] = False
    primes = []

    for (i, isprime) in enumerate(status):
        if isprime:
            primes.append(i)
            for n in range(i*i, limit, i):     
                status[n] = False

    return primes

In [12]:
primes(57140)[-2]


Out[12]:
57131

In [6]:
max_num = int(input('Calculate prime numbers under: '))
for n in primes(max_num):
    print(n, end = " ")


Calculate prime numbers under: 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

To make this even faster, you could exclude all even numbers from the beginning on and start with 2 in the list of primes.